/*

 */
package com.insigma.business.qrcode.common.template;

import com.insigma.business.qrcode.common.util.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


@Service("localCacheManager")
public class CacheManager {

    public final Logger logger = LoggerFactory.getLogger(getClass());

    
    private final static String POSTFIX_LAST_UPDATE_SEC = ":LAST_UPDATE_SEC";

    
    private final static long INDEFINITE_EXPIRY = -1L;

    
    public synchronized String getCommonPropFileValue(final String propFileName, final String key) {
        Properties properties = DataCache.getInstance().getCommonProperties(propFileName);
        if (properties == null) {
            properties = new Properties();
            InputStream inputStream = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(propFileName));
            try {
                properties.load(inputStream);
                DataCache
                        .getInstance().setCommonProperties(propFileName, properties);
            } catch (Exception e) {
                try {
                    inputStream.close();
                } catch (IOException ee) {
                    ExceptionUtil
                            .caught(logger,e,"DataCache.getCommonPropFileValue.closeStream error");
                }
                ExceptionUtil.caught(logger,e,"DataCache.getCommonPropFileValue error");
            }
        }

        return (String) properties.get(key);
    }

    
    public synchronized Object getCommonValue(final String key) {
        return getCommonValue(key, INDEFINITE_EXPIRY);
    }

    
    public synchronized Object getCommonValue(final String key, final long timeoutSec) {
        if (timeoutSec == INDEFINITE_EXPIRY) {
            return getCachedCommonValue(key);
        } else {
            final Object lastUpdateSecObj = DataCache.getInstance().getCommonValue(key + POSTFIX_LAST_UPDATE_SEC);
            if (lastUpdateSecObj == null || (Long.parseLong(lastUpdateSecObj.toString()) + timeoutSec) < System.currentTimeMillis() / 1000) {
                removeCommonValue(key);
                return null;
            } else {
                return getCachedCommonValue(key);
            }
        }
    }

    
    private Object getCachedCommonValue(final String key) {
        Object obj = DataCache.getInstance().getCommonValue(key);
        if (obj != null) {
            DataCache
                    .getInstance().setCommonValue(key + POSTFIX_LAST_UPDATE_SEC, System.currentTimeMillis() / 1000);
            return obj;
        } else {
            return null;
        }
    }

    
    public synchronized void setCommonValue(final String key, final Object value) {
        DataCache.getInstance().setCommonValue(key, value);
        DataCache.getInstance().setCommonValue(key + POSTFIX_LAST_UPDATE_SEC, System.currentTimeMillis() / 1000);
    }

    
    public synchronized void removeCommonValue(final String key) {
        DataCache.getInstance().removeCommonValue(key);
        DataCache.getInstance().removeCommonValue(key + POSTFIX_LAST_UPDATE_SEC);
    }

}
